package com.link.play.fit.ai.ui.guide.ac

import android.graphics.Color
import android.os.Bundle
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.databinding.AcWelcomeBinding
import com.link.play.fit.ai.kv.AuthCache.isVip
import com.link.play.fit.ai.ui.guide.BasePageFragment
import com.link.play.fit.ai.ui.guide.RateFg2
import com.link.play.fit.ai.ui.guide.WelcomePageFg1
import com.link.play.fit.ai.ui.guide.WelcomePageFg2
import com.link.play.fit.ai.ui.guide.WelcomePrivacyFg
import com.link.play.fit.ai.ui.vip.VipBuyDesAc
import com.link.play.fit.ai.ui.vip.VipBuyDesAc2
import com.zhpan.indicator.enums.IndicatorSlideMode
import com.zhpan.indicator.enums.IndicatorStyle

class WelcomeAc : BaseActivity<AcWelcomeBinding>(AcWelcomeBinding::inflate) {


    private val page1 by lazy {
        WelcomePageFg1().apply {
            pageIndex = 0
        }
    }

    private val page2 by lazy {
        WelcomePageFg2().apply {
            pageIndex = 1
        }
    }

    private val page3 by lazy {
        RateFg2().apply {
            pageIndex = 2
        }
    }
    private val page4 by lazy {
        WelcomePrivacyFg().apply {
            pageIndex = 3
        }
    }

    var currentIndex = 0

    var isAgree = true

    private val fragments: MutableList<Fragment> by lazy {
        mutableListOf(
            page1, page2,
//            page3,
//            page4
        )
    }

    override fun useDefaultImmersive() {
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom)
            insets
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        fragments.forEachIndexed { index, fragment ->
            (fragment as BasePageFragment<*>).pageIndex = index
        }

        initViewPager2()

        binding.flBtn.setOnClickListener {
            if (currentIndex == page2.pageIndex) {
                if (isAgree) {
                    goToAc(GuideHelloAc::class.java)
//                    if (isVip) {
//                        goToAc(GuideHelloAc::class.java)
//                    } else {
//                        goToAc(VipBuyDesAc2::class.java)
//                    }
                    finish()
                } else {
                    Toast.makeText(this, getString(R.string.please_agree_terms), Toast.LENGTH_SHORT).show()
                }
            } else {
                val nextIndex = currentIndex + 1
                binding.viewPager2.setCurrentItem(nextIndex, true)
//                if (nextIndex == page3.pageIndex) {
//                    (page3).startReviewFlow(this)
//                }
            }
        }
    }

    private fun initViewPager2() {
        binding.indicatorView.apply {

            setSliderColor(
                Color.WHITE,
                ContextCompat.getColor(this@WelcomeAc, R.color.colorPrimary)
            )
            setSliderWidth(resources.getDimension(R.dimen.dp_10))
            setSliderHeight(resources.getDimension(R.dimen.dp_10))
            setSlideMode(IndicatorSlideMode.NORMAL)
            setIndicatorStyle(IndicatorStyle.CIRCLE)
            setPageSize(fragments.size)
            notifyDataChanged()

        }

        binding.viewPager2.run {
            isUserInputEnabled = true
            adapter = object : FragmentStateAdapter(this@WelcomeAc) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }
        binding.viewPager2.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                binding.indicatorView.onPageScrolled(position, positionOffset, positionOffsetPixels)

            }

            override fun onPageSelected(position: Int) {
                currentIndex = position
                binding.indicatorView.onPageSelected(position)
                changeAcUI(position)
            }
        })
    }

    private fun changeAcUI(position: Int) {
    }

}